﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PatientsManagement.Database;
using System.Configuration;
using PatientsManagement.Users;

namespace PatientsManagement
{
    public partial class ManageUsers : Form
    {
        DataTable dataTable = new DataTable();
        string username;

        public ManageUsers(string username,string name)
        {
            InitializeComponent();
            user.Text = name;
            this.username = username;
            FillGridWithUsersData();
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Escape)
                this.Dispose();
        }
        private void FillGridWithUsersData()
        {
            dataView.DataSource = UsersData.GetUsersTable();
            int count = dataView.ColumnCount;
            columnsCombo.Items.Clear();
            dataTable = new DataTable();
            for (int i = 0; i < dataView.ColumnCount; i++)
            {
                columnsCombo.Items.Add(dataView.Columns[i].Name);
                if(dataView.Columns[i].HeaderText == "Aktywny")
                    dataTable.Columns.Add(dataView.Columns[i].Name,typeof(bool));
                else
                    dataTable.Columns.Add(dataView.Columns[i].Name, typeof(string));

                if (dataView.Columns[i].HeaderText == "Aktywny")
                {
                    dataTable.Columns[i].ReadOnly = false;
                    dataView.Columns[i].ReadOnly = false;
                }
                else
                {
                    dataTable.Columns[i].ReadOnly = true;
                    dataView.Columns[i].ReadOnly = true;
                }
            }
            for (int i = 0; i < dataView.Rows.Count; i++)
            {
                dataTable.Rows.Add(dataView.Rows[i].Cells["ID"].Value.ToString(),
                    dataView.Rows[i].Cells["Nazwa"].Value.ToString(),
                    dataView.Rows[i].Cells["Imię"].Value.ToString(),
                    dataView.Rows[i].Cells["Nazwisko"].Value.ToString(),
                    dataView.Rows[i].Cells["Typ"].Value.ToString(),
                    dataView.Rows[i].Cells["Aktywny"].Value.ToString());
            }
            columnsCombo.SelectedIndex = 0;
            filterValue.Text = "";
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void filterButton_Click(object sender, EventArgs e)
        {
            dataTable.DefaultView.RowFilter = string.Format("{0} LIKE '%{1}%'", columnsCombo.Text, filterValue.Text);
            dataView.DataSource = dataTable;            
        }

        private void add_Click(object sender, EventArgs e)
        {
            AddUser addUser = new AddUser(true);
            if (addUser.ShowDialog() == DialogResult.OK)
            {
                addUser.AddToDatabase();
                FillGridWithUsersData();
            }
        }

        private void saveChanges_Click(object sender, EventArgs e)
        {
            List<string> usernames = new List<string>();
            List<bool> active = new List<bool>();
            for (int i = 0; i < dataView.Rows.Count; i++)
            {
                usernames.Add(dataView.Rows[i].Cells["Nazwa"].Value.ToString());
                active.Add((bool)dataView.Rows[i].Cells["Aktywny"].Value);
            }
            UsersData.UpdateUsersTable(usernames,active);
            MessageBox.Show("Zmiany zostały zapisane");
        }

        private void dataView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (dataView.Rows[e.RowIndex].Cells["Nazwa"].Value.ToString().ToLower() == username.ToLower())
                e.Cancel = true;
        }

        private void editUser_Click(object sender, EventArgs e)
        {
            string selectedUsername = "", selectedForename = "", selectedSurname = "", selectedType = "";
            selectedUsername = dataView.SelectedRows[0].Cells["Nazwa"].Value.ToString();
            selectedForename = dataView.SelectedRows[0].Cells["Imię"].Value.ToString();
            selectedSurname = dataView.SelectedRows[0].Cells["Nazwisko"].Value.ToString();
            selectedType = dataView.SelectedRows[0].Cells["Typ"].Value.ToString();

            AddUser addUser = new AddUser(selectedUsername, selectedForename, selectedSurname, selectedType,username);
            if (addUser.ShowDialog() == DialogResult.OK)
            {
                addUser.SaveChangesToDatabase();
                FillGridWithUsersData();
            }
        }

        private void deleteUser_Click(object sender, EventArgs e)
        {
            string selectedUsername =  dataView.SelectedRows[0].Cells["Nazwa"].Value.ToString();
            if(selectedUsername.ToLower() == username.ToLower())
            {
                MessageBox.Show("Usunięcie własnego konta nie jest możliwe.");
            }else
            {
                if (DialogResult.Yes == MessageBox.Show("Na pewno chcesz usunąć użytkownika " + selectedUsername.ToLower() + "?", "Usuwanie użytkownika", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))
                {
                    UsersData.DeleteUser(selectedUsername);
                    MessageBox.Show("Użytkownik " + selectedUsername.ToLower() + " został usunięty.");
                    FillGridWithUsersData();
                }
            }
        }

        private void dataView_SelectionChanged(object sender, EventArgs e)
        {
            if (dataView.SelectedRows.Count > 0)
            {
                if (dataView.SelectedRows[0].Cells["Typ"].Value.ToString() == "Lekarz")
                {
                    link.Enabled = true;
                    if ((bool)dataView.SelectedRows[0].Cells["Powiązany"].Value)
                        deleteLink.Enabled = true;
                }
                else
                {
                    deleteLink.Enabled = false;
                    link.Enabled = false;
                }
            }
        }

        private void link_Click(object sender, EventArgs e)
        {
            int userID = (int)dataView.SelectedRows[0].Cells["ID"].Value;
            string foreName = dataView.SelectedRows[0].Cells["Imię"].Value.ToString();
            string surName = dataView.SelectedRows[0].Cells["Nazwisko"].Value.ToString();
            LinkDoctor linkDoctor = new LinkDoctor(userID,foreName,surName);
            if (linkDoctor.ShowDialog() == DialogResult.OK)
            {
                linkDoctor.SaveChangesToDatabase();
                FillGridWithUsersData();
            }
        }

        private void deleteLink_Click(object sender, EventArgs e)
        {
            DoctorsData.DeleteDoctorLink((int)dataView.SelectedRows[0].Cells["ID"].Value);
            FillGridWithUsersData();
        }
    }
}
